//
//  XPlanEditViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/8/29.
//

#import "XPlanEditViewController.h"
#import <Realm/Realm.h>

#import "XTextField.h"

@interface XPlanEditViewController ()<UITextViewDelegate>

@property(nonatomic,assign)NSUInteger maxLenth;//100

//@property(nonatomic,strong)MyRelativeLayout* navbar;
//
///// title
//@property(nonatomic,strong)UILabel* titleLb;
//
///// x
//@property(nonatomic,strong)UIButton* closeBtn;



@property(nonatomic,strong) MyLinearLayout *contentLayout;

/// 名称
@property(nonatomic,strong)XTextField* nameTextField;

/// 数量
@property(nonatomic,strong)XTextField* countTextField ;

/// 占位文本
@property(nonatomic,strong)UILabel* placeLb;

/// 字数
@property(nonatomic,strong)UILabel* numLb;

/// 输入框
@property (nonatomic,weak)UITextView* textView;



@end

@implementation XPlanEditViewController

- (instancetype)init{
    if(self = [super init]){
        self.maxLenth = 100;
    }
    return self;
}

- (void)loadView{
    [super loadView];
    // 线性布局
    MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    rootLayout.backgroundColor = COLOR_BACKGEOUND;
    self.view = rootLayout;
    
//    MyRelativeLayout* navbar = [MyRelativeLayout new];
//    navbar.myHeight = IPHONE_NAVIGATION_BAR_HEIGHT;
//    navbar.myMargin = 0;
//    navbar.bottomBorderline = [[MyBorderline alloc] initWithColor:COLOR_GRAY_QUITE];
//    [rootLayout addSubview:navbar];
//    _navbar = navbar;
    
    //底部滚动控件
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.myMargin = 0;
    scrollView.weight = 1.0;
    [rootLayout addSubview:scrollView];
    
    // 线性布局
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contentLayout.padding = UIEdgeInsetsZero;
    contentLayout.myHorzMargin = 0;
    contentLayout.myTop = 0;
    contentLayout.heightSize.lBound(scrollView.heightSize, 10, 1);
    [scrollView addSubview:contentLayout];
    _contentLayout = contentLayout;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavBar];
    
    [self setupViews];
    
    if(self.operation == EditOperation_Edit){
        [self updateViews];
    }
}

- (void)setupNavBar{
    
    NSString* time;
    switch (self.dateType) {
        case ADateType_Day:
            time = @"日";
            break;
        case ADateType_Week:
            time = @"周";
            break;
        case ADateType_Month:
            time = @"月";
            break;
        case ADateType_Year:
            time = @"年";
            break;
        default:
            break;
    }
    NSString* title;
    if(self.operation == EditOperation_Edit){
        title = [NSString stringWithFormat:@"编辑每%@计划",time];
    }else{
        title = [NSString stringWithFormat:@"添加每%@计划",time];
    }
    self.navigationItem.title = title;
    
//    UIBarButtonItem* closeItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:(UIBarButtonItemStylePlain) target:self action:@selector(confimClicked:)];
//    self.navigationItem.rightBarButtonItem = closeItem;
    
}

- (void)setupViews{
    
    NSAttributedString *attrString1 = [[NSAttributedString alloc] initWithString:@"请输入计划名称" attributes: @{NSForegroundColorAttributeName:COLOR_GRAY_QUITE}];
    
    XTextField* nameTextField = [[XTextField alloc] init];
    nameTextField.myHorzMargin = 16;
    nameTextField.myTop = 20;
    nameTextField.myHeight = PX(44);
    nameTextField.textField.attributedPlaceholder = attrString1;
    [self.contentLayout addSubview:nameTextField];
    _nameTextField = nameTextField;
    
    NSAttributedString *attrString2 = [[NSAttributedString alloc] initWithString:@"请输入计划目标数量" attributes: @{NSForegroundColorAttributeName:COLOR_GRAY_QUITE}];
    
    XTextField* countTextField = [[XTextField alloc] init];
    countTextField.myHorzMargin = 16;
    countTextField.myTop = 16;
    countTextField.myHeight = PX(44);
    countTextField.textField.attributedPlaceholder = attrString2;
    [self.contentLayout addSubview:countTextField];
    _countTextField = countTextField;
    
    MyRelativeLayout* subLayout = [MyRelativeLayout new];
    subLayout.backgroundColor = UIColor.whiteColor;
    subLayout.myHorzMargin = 16;
    subLayout.myTop = 16;
    subLayout.myHeight = MyLayoutSize.wrap;
    subLayout.layer.cornerRadius = PX(5);
    subLayout.layer.masksToBounds = YES;
    [self.contentLayout addSubview:subLayout];
    
    UITextView* textView = [UITextView new];
    textView.backgroundColor = UIColor.whiteColor;
    textView.font = FONT_FIT(14);
    textView.textColor = COLOR_BLACK_DARK;
    textView.delegate = self;
    textView.scrollEnabled = NO;
    textView.returnKeyType = UIReturnKeyDone;
    textView.showsHorizontalScrollIndicator = NO;
    textView.showsVerticalScrollIndicator = NO;
    textView.tintColor = COLOR_THEME_A;
    textView.myMargin = 0;
    textView.myHeight = PX(100);
    [subLayout addSubview:textView];
    _textView = textView;
    
    UILabel* placeLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_GRAY_QUITE text:@"给自己一句鼓励额话吧"];
    placeLb.topPos.equalTo(textView.topPos).offset(8);
    placeLb.leftPos.equalTo(textView.leftPos).offset(8);
    [subLayout addSubview:placeLb];
    _placeLb = placeLb;
    
    UILabel* numLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_QUITE text:@"0/100"];
    numLb.myRight = 8;
    numLb.topPos.equalTo(textView.bottomPos).offset(8);
    [subLayout addSubview:numLb];
    _numLb = numLb;
    
    
    UIButton* confimBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confimBtn.backgroundColor = COLOR_THEME_A;
    [confimBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confimBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [confimBtn addTarget:self action:@selector(confimClicked:) forControlEvents:UIControlEventTouchUpInside];
    confimBtn.titleLabel.font = FONT_FIT(14);
    confimBtn.layer.cornerRadius = PX(4);
    confimBtn.layer.masksToBounds = YES;
    confimBtn.myHeight = PX(40);
    confimBtn.myHorzMargin = 16;
    confimBtn.myTop = 32;
    [self.contentLayout addSubview:confimBtn];
}

- (void)updateViews{
    
    self.nameTextField.textField.text = self.model.name;
    
    self.countTextField.textField.text = [NSString stringWithFormat:@"%ld",self.model.planCount];
    
    self.textView.text = self.model.desc;
    self.placeLb.visibility = self.model.desc.length?MyVisibility_Gone:MyVisibility_Visible;
    [self.placeLb sizeToFit];
    
    self.numLb.text = [NSString stringWithFormat:@"%ld/%ld",self.model.desc.length,self.maxLenth];
    [self.numLb sizeToFit];
}

#pragma mark  --- action
///关闭
- (void)closeClicked:(UIButton*)sender{
    [self closeViewControllerAnimated:YES];
}

///确定
- (void)confimClicked:(UIButton*)sender{
    
    NSString* name = self.nameTextField.textField.text;
    name = [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    NSString* count = self.countTextField.textField.text;
    count = [count stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    NSString* desc = self.textView.text;
    desc = [desc stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if(!name.length){
        [Toast toast:self.nameTextField.textField.placeholder];
        return;
    }
    if(!count.length){
        [Toast toast:self.countTextField.textField.placeholder];
        return;
    }
        
    if(self.operation == EditOperation_Add){

    
        XPlanModel* model = [XPlanModel new];
        model.dateType = self.dateType;
        model.name = name;
        model.planCount = [count integerValue];
        model.desc = desc;
        
        [[RLMRealm defaultRealm] beginWriteTransaction];
        [[RLMRealm defaultRealm] addObject:model];
        [[RLMRealm defaultRealm] commitWriteTransaction];
        
        if(self.editBlock){
            self.editBlock(model, self.operation);
        }

    }else if (self.operation == EditOperation_Edit){
        
        [[RLMRealm defaultRealm] beginWriteTransaction];
        self.model.name = name;
        self.model.planCount = [count integerValue];
        self.model.desc = desc;
        [[RLMRealm defaultRealm] addOrUpdateObject:self.model];
        [[RLMRealm defaultRealm] commitWriteTransaction];
        
        if(self.editBlock){
            self.editBlock(self.model, self.operation);
        }
        
    }
    [self closeViewControllerAnimated:YES];
}


#pragma mark  -- UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView{
    
    self.placeLb.visibility = textView.text.length?MyVisibility_Gone:MyVisibility_Visible;
    [self.placeLb sizeToFit];
    
    if(textView.markedTextRange != NULL){
        return;
    }
    
    NSString* text = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if(text.length > self.maxLenth){
        text = [text substringToIndex:self.maxLenth];
        textView.text = text;
    }
   
    self.numLb.text = [NSString stringWithFormat:@"%ld/%ld",text.length,self.maxLenth];
    [self.numLb sizeToFit];
}
@end
